<template>
 <div  class="components-container">
        <sweet-modal ref="dialog" class="dialog">
            <h3>{{ title }}</h3>
            <div style="margin:30px 50px">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">编码名称</label>
                        <div class="layui-input-inline">
                            <el-input type="text" v-model="selectData.CODALIAS" :disabled="isshow"></el-input>  
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">编码备注</label>
                        <div class="layui-input-inline">
                            <el-input type="text" v-model="selectData.CODDESC" :disabled="isshow"></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">序号</label>
                        <div class="layui-input-inline">
                            <el-input type="text" v-model="selectData.CODNUM" :disabled="isshow"></el-input>
                        </div>
                    </div>
                    <div class="layui-inline" v-if="isshowstatus">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline">
                            <el-select v-model="selectData.CODSTATUS" placeholder="请选择状态" :disabled="isshow">
                                <el-option  key="A" label="正常" value="A"></el-option>
                                <el-option  key="B" label="禁用" value="B"></el-option>
                            </el-select>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item btnCenter">
              <el-button @click="closeDialog">取消</el-button>
              <el-button type="primary" @click="submit" :loading="loading">确定</el-button>
            </div>
        </sweet-modal>
 </div>
</template>

<script>
import sweetModal from "@/components/sweetModal";
import apiBase from "@/api/apiBase.js";
const ADD = "新增";
const MODIFY = "修改";
const DELETE = "删除";
const INFO = "详情";
export default {
  name: "DepartmentMain",
  data() {
    return {
        title: null,
        states: [],
        options: [],
        loading: false,
        selectData: {
            CODALIAS: null,
            CODDESC: null,
            CODNUM: null,
            CODSTATUS: 'A'
        },
        isshow: false,
        isshowstatus: true,
        current: null,
        CodCtpIdEquals: null,
        copycodename: null,
        CODORGID: null,
    };
  },
  methods: {
    //功能操作
    open(order,CodCtpIdEquals, current) {
      this.order = order;
      this.dataRepeat();
      if(current){
          this.current = current;
      }
      if(CodCtpIdEquals){
          this.CodCtpIdEquals = CodCtpIdEquals;
      }
      this.isshowstatus = true;
      this.isshow = false;
      switch (this.order) {
        case ADD:
          this.title = "新增";
            this.isshowstatus = false;
          this.$refs.dialog.open();
          break;
        case MODIFY:
          this.title = "修改";
          this.dataInfo(current.CODNAME);
          this.$refs.dialog.open();
          break;
        case INFO:
          this.title = "详情";
          this.isshow = true;
          this.dataInfo(current.CODNAME);
          this.$refs.dialog.open();
          break;
        // case DELETE:
        //   this.dataDelete(current.CODNAME);
        //   break;
        default:
      }
    },
    //提交
    submit() {
      switch (this.order) {
        case ADD:
          this.dataAdd();
          break;
        case MODIFY:
          this.dataModify();
          break;
        default:
      }
    },

    //数据重置
    dataRepeat() {
      Object.assign(this.$data.selectData,this.$options.data().selectData);
    },
    //新增提交
    dataAdd() {
      this.loading = true;
      let Data = JSON.parse(JSON.stringify(this.selectData));
      Data.CODCTPID = this.CodCtpIdEquals;    
      Data.CODNAME = this.selectData.CODALIAS;    
      apiBase.DoService(
        Data,
        apiBase.getApiAdress().Code.Create,
        res => {
          this.loading = false;
          this.$refs.dialog.close();

          this.$message({
            type: "success",
            message: "新增成功!"
          });
          this.refreshData();
        },
        res => {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
      );
    },
    // 删除
    // dataDelete(CODNAME) {
    //   this.$confirm("确认删除该数据?", "提示", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   })
    //     .then(callback => {
    //       let data = {
    //             CODNAME: CODNAME
    //         };
    //       apiBase.DoService(data, apiBase.getApiAdress().Dept.Delete, res => {
    //         this.$message({
    //           type: "success",
    //           message: "删除成功!"
    //         });
    //         this.refreshData();
    //       });
    //     })
    //     .catch(() => {
    //       this.$message({
    //         type: "info",
    //         message: "已取消删除"
    //       });
    //     });
    // },
    //修改提交
    dataModify() {
      this.loading = true;
      let Data = {};
      Data.CODCTPID = this.CodCtpIdEquals;
      Data.CODORGID = this.CODORGID;
      Data.CODALIAS = this.selectData.CODALIAS;
      Data.CODNAME = this.copycodename;
      Data.CODDESC = this.selectData.CODDESC;
      Data.CODNUM = this.selectData.CODNUM;
      Data.CODSTATUS = this.selectData.CODSTATUS;
      apiBase.DoService(
        Data,
        apiBase.getApiAdress().Code.Modify,
        res => {
          this.loading = false;
          this.$refs.dialog.close();
          this.$message({
            type: "success",
            message: "修改成功!"
          });
          this.refreshData();
        },
        res => {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
      );
    },

    //详情
    dataInfo(CODNAME) {
      let data = {
        CODNAME: CODNAME,
        CODCTPID: this.CodCtpIdEquals
      };
      apiBase.DoService(data, apiBase.getApiAdress().Code.Detail, res => {
        this.selectData = res.data.content;
        this.copycodename = res.data.content.CODNAME;
        this.CODORGID = res.data.content.CODORGID;
      });
    },
    closeDialog() {
      this.$refs.dialog.close();
    },

    refreshData() {
      this.$emit("refreshData");
    }
  },
  components: {
    sweetModal
  }
};
</script>

<style>

</style>
